
using System;
using System.Collections.Generic;
using Godot;

class EventManager
{

    public static EventManager _instance;

    public Dictionary<string, EventHandler<EventParams>> eventDict = new Dictionary<string, EventHandler<EventParams>>();

    public static EventManager getInstance()
    {
        if (_instance is null)
        {
            _instance = new EventManager();
        }
        return _instance;
    }


    // 注册事件
    public void addListener(string events, EventHandler<EventParams> e)
    {
        if (!eventDict.ContainsKey(events))
        {
            eventDict.Add(events, e);
        }
        else
        {
            eventDict[events] += e;
        }


    }

    //移除事件
    public void removeListener(string events, EventHandler<EventParams> e)
    {
        if (eventDict.ContainsKey(events))
        {
            eventDict[events] -= e;
        }
    }

    //发送事件
    public void dispatchEvent(string events, EventParams data = null)
    {
        if (eventDict.ContainsKey(events))
        {
            if (eventDict[events] is not null)
            {

                if (data is not null)
                {
                    eventDict[events](this, data);
                }
                else
                {

                    eventDict[events](this, null);
                }

            }
        }

    }
}


class EventParams : EventArgs
{
    public Dictionary<string, object> paramDict = new Dictionary<string, object>();

}
